package example.simple_mindmap.consistency;

import example.simple_mindmap.common.designs.theadPool.BulkExecuteHandler;
import example.simple_mindmap.common.designs.theadPool.BulkExecuteUtil;
import example.simple_mindmap.common.designs.theadPool.BulkTaskCollection;
import example.simple_mindmap.common.designs.theadPool.strategy.error.RetryAndCollectError;
import example.simple_mindmap.common.designs.theadPool.strategy.name.AutoIncrementRename;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @Program: 8.mindmap
 * @Author: 海里的鱼
 * @Create: 2024-08-28 08:59
 * @Name: 数据一致性的处理器
 * @Desc: TODO
 */

public class ConsistencyHandler {
    //批量执行处理器（线程池）
    private BulkExecuteHandler<ConsistencyTask> handler;
    //写入数据列表
    private List<ConsistencyData> dataList;
    //写入失败的数据
    private List<ConsistencyData> failList ;

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 构造方法
     * @Describe: TODO
     **/
     public ConsistencyHandler(List<ConsistencyData> dataList) {
        this.handler = BulkExecuteUtil.getHandler(new RetryAndCollectError(ConfigVar.MAX_RETRY,ConfigVar.INTERVAL));
        this.dataList = dataList;
        this.failList = new ArrayList<ConsistencyData>();
    }

    public ConsistencyHandler() {
        this.handler = BulkExecuteUtil.getHandler(new RetryAndCollectError(ConfigVar.MAX_RETRY,ConfigVar.INTERVAL));
        this.dataList = new ArrayList<ConsistencyData>();
        this.failList = new ArrayList<ConsistencyData>();
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 执行数据的一致性
     * @Describe: 根据指定【方案】操作【ConsistencyData】
     **/
    @Transactional
    public Boolean executeConsistency(){
         failList = new ArrayList<ConsistencyData>();

         BulkTaskCollection<ConsistencyTask> taskList = BulkExecuteUtil.getTaskCollection(ConsistencyTask.class);
         taskList.setRenameStrategy(new AutoIncrementRename(1,"Task:"));
         for (ConsistencyData data : dataList) {
             if(null == data.getWriteFunc()){
                 new ConsistencyException("【"+data.getType().getMessage()+"】的一致性任务执行失败，因为执行方法为空").printStackTrace();
                 return false;
             }
             if(null == data.getScheme()){
                 new ConsistencyException("【"+data.getType().getMessage()+"】的一致性任务执行失败，因为方案为空").printStackTrace();
                 return false;
             }
             if(null == data.getData()){
                 new ConsistencyException("【"+data.getType().getMessage()+"】的一致性任务执行失败，因为数据为空").printStackTrace();
                 return false;
             }
             ConsistencyTask task = new ConsistencyTask(data);
             taskList.add(task);
         }
         handler.setTaskCollection(taskList);
         handler.executeTasks();
         System.out.println(handler.getExecuteInfo());
         return handler.getErrorTasks().size() <= 0;
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 添加
     * @Describe: TODO
     **/
     public Boolean addConsistencyData(ConsistencyData data) {
        if(null == dataList) {
            return false;
        }
        return dataList.add(data);
    }

    /** ---------------------------------------------------------------------------------------------------------------------
     * @Method  : 返回失败的写入数据
     * @Describe: TODO
     **/

    public List<ConsistencyData> getFailList(){
        if(null == handler.getErrorTasks()) {
            return new ArrayList<ConsistencyData>();
        }
        BulkTaskCollection<ConsistencyTask> taskCollection = handler.getTaskCollection();
        for(String name: handler.getErrorTasks()){
            failList.add(taskCollection.get(name).getData());
        }
        return failList;
    }

}
